package libs

import (
	"exercise/application/yiran/app/libs/errors"
	"github.com/gin-gonic/gin"
	"net/http"
)

// ResResponse  结果响应
func ResResponse(ctx *gin.Context, code int, messages string, data interface{}) {
	ctx.JSON(http.StatusOK, gin.H{
		"code":     code,
		"messages": messages,
		"data":     data,
	})
}

// Success 请求成功
func Success(ctx *gin.Context, data interface{}) {
	ResResponse(ctx, errors.SuccessCode, "请求成功", data)
}

// Toast 请求失败
func Toast(ctx *gin.Context, messages string) {
	if messages == "" {
		messages = "请求失败"
	}
	ResResponse(ctx, errors.ErrorCode, messages, "")
}

// OkToast 请求失败
func OkToast(ctx *gin.Context, messages string) {
	if messages == "" {
		messages = "请求成功"
	}
	ResResponse(ctx, errors.SuccessCode, messages, "")
}

// TokenToast 用户信息token校验错误
func TokenToast(ctx *gin.Context, messages string) {
	if messages == "" {
		messages = "请求失败"
	}
	ResResponse(ctx, errors.AuthorizationError, messages, "")
}

// ServiceException 服务异常
func ServiceException(ctx *gin.Context, messages string, data interface{}) {
	ResResponse(ctx, errors.ServerError, messages, data)
}

// Failure 请求失败
func Failure(c *gin.Context, err *errors.Error) {
	ResResponse(c, err.GetCode(), err.GetMessage(), "")
}
